﻿<%@ WebHandler Language="C#" Class="WXActivity.ActivityHandler" %>

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Web;
using Activity;
using Activity.API;
namespace WXActivity
{
    /// <summary>
    /// 抽奖活动处理程序
    /// </summary>
    public class ActivityHandler : BaseHandler
    {

        public ActivityHandler()
        {
            //修改请求为post 方式
            base._httpReuqest = base.Context.Request.Form;
        }
        #region  2018活动
        /// <summary>
        /// 校验用户，成功返回积分
        /// </summary>
        public void InitUser()
        {
            //var date = DateTime.Now;
            var code = _httpReuqest["code"];
            if (string.IsNullOrWhiteSpace(code))
            {
                Error("无效的[Code]值", -1);
            }
            else
            {
                //Log.WriteLog("请求openId开始："+date.ToString(CultureInfo.InvariantCulture));
                var user = WeixinApi.GetOpenID(code);
                //Log.WriteLog("请求openId结束："+date.ToString(CultureInfo.InvariantCulture));
                if (user == null || user.openid == null)
                {
                    Error("用户不存在", -2);
                }
                else
                {
                    //Log.WriteLog("请求用户信息开始："+date.ToString(CultureInfo.InvariantCulture));
                    var lottery2018 = new Lottery2018Handler();
                    var result = lottery2018.GetUser(user.openid);
                    //Log.WriteLog("请求用户信息结束："+date.ToString(CultureInfo.InvariantCulture));
                    if (result != null)
                        Success(result);
                    else
                        Error("请先注册会员", -3);
                }
            }


        }

        /// <summary>
        /// 返回用户积分
        /// </summary>
        public void GetUserIntegral()
        {
            var openId = _httpReuqest["openId"];
            var lottery2018 = new Lottery2018Handler();
            var result = lottery2018.GetUser(openId);
            if (result != null)
                Success(result);
            else
                Error("会员不存在", -3);
        }

        /// <summary>
        /// 51,夏季积分抽奖
        /// </summary>
        public void Lottery2018Integral()
        {
            var type = _httpReuqest["type"];
            var openId = _httpReuqest["openId"];
            if (!string.IsNullOrWhiteSpace(type))
            {
                var handler = new Lottery2018Handler();
                Success(handler.Lottery(Convert.ToInt16(type), openId));
            }
            else
                Error("类型错误");
        }

        /// <summary>
        /// 中奖列表
        /// </summary>
        public void List()
        {
            var type = _httpReuqest["type"];
            var openId = _httpReuqest["openId"];
            if (!string.IsNullOrWhiteSpace(type))
            {
                var handler = new Lottery2018Handler();
                Success(handler.List(openId, Convert.ToInt16(type)));
            }
            else
                Error("类型错误");
        }

        /// <summary>
        /// 中奖列表
        /// </summary>
        public void SummaryList()
        {
            var type = _httpReuqest["type"];
            if (!string.IsNullOrWhiteSpace(type))
            {
                var handler = new Lottery2018Handler();
                Success(handler.WelfareSummary(Convert.ToInt32(type)));
            }
            else
                Error("类型错误");
        }
        #endregion

        /********************************520会员狂欢周活动方案begin**********************************/
        public void Lottery520()
        {
            var openId = _httpReuqest["openId"];
            if (!string.IsNullOrWhiteSpace(openId))
            {
                var d=new HappyWeek520Handler();
                var msg= d.Lottery(openId);
                if (msg.Code != 1 || msg.Code!=2)
                {
                    msg.IsSuc = false;
                    msg.Message = messageCode[msg.Code];
                }
                else
                {
                    msg.IsSuc = true;
                    //msg.Message = msg.Message;
                }
                Success(msg);
            }
            else
                Error("类型错误");
        }
        /********************************520会员狂欢周活动方案end**********************************/

     
    }
}